package cn.ticket.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Department;
import cn.ticket.service.DepartmentService;
import cn.ticket.service.Utility;

@Controller
@RequestMapping("department")
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
@RequestMapping(value="getAll",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getAll(){
	Map<String, Object> result=new HashMap<>();
	List<Department> departments=departmentService.getAll();
	if (!departments.isEmpty()) {
		result.put("status", 1001);
		result.put("body", departments);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String insert(@RequestBody Department record){
	Map<String, Object> result=new HashMap<>();
	int num=departmentService.insertSelective(record);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String update(@RequestBody Department record){
	Map<String, Object> result=new HashMap<>();
	int num=departmentService.updateByPrimaryKeySelective(record);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="delete",produces={"application/json;charset=utf-8"})
@ResponseBody
public String delete(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int id=Integer.parseInt(args.get("id"));
	int num=departmentService.deleteByPrimaryKey(id);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
}
